package com.gitee.cnlongdb.server.datasync;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.gitee.cnlongdb.basic.entity.datasync.CnLongDataSyncRequest;
import com.gitee.cnlongdb.basic.entity.datasync.CnLongDataSyncResponse;
import com.gitee.cnlongdb.basic.enums.sysnc.LongdbDataSyncEnms;
import com.gitee.cnlongdb.server.connect.nettymission.LongdbNettyClient;

/**
 * 系统名称：DataSyncConnectMessage.java
 * 模块名称：
 * 模块描述：消息同步
 * 功能列表：
 * 模块作者：ZOUYONG
 * 开发时间：2019年4月3日 下午9:03:30
 * 模块路径：com.gitee.cnlongdb.server.datasync.DataSyncConnectMessage
 * 更新记录：
 */
public class DataSyncConnectMessage {

	private static final Logger logger = (Logger) LoggerFactory.getLogger(DataSyncConnectMessage.class);

	/**
	 * 功能描述：开始消息同步
	 * 模块作者：ZOUYONG
	 * 开发时间：2019年4月3日 下午9:05:44
	 * 更新记录：
	 * 返回数据：String
	 */
	public static CnLongDataSyncResponse noticeDataSyncSend(String serverIp, Integer serverPort, CnLongDataSyncRequest dataSync) {
		
		CnLongDataSyncResponse response = new CnLongDataSyncResponse();
		logger.info("开始消息同步，customerIp：{}; customerPort: {}; longMqConfig", 
				serverIp, serverPort, null != dataSync ? JSON.toJSONString(dataSync) : null);
		if(StringUtils.isBlank(serverIp) || null == serverPort || null == dataSync || !dataSync.check()) {
			response.setReturnCode(LongdbDataSyncEnms.fail.name());
			response.setReturnMsg("要同步的参数不能为空！");
			return response;
		}
		
		try {
			Map<String, String> sendMap = LongdbNettyClient.sendNettyMessageByThirdSystem(serverIp, serverPort, dataSync);
			response.setJsonData(sendMap.get(LongdbDataSyncEnms.returnObject.name()));
			response.setReturnCode(sendMap.get(LongdbDataSyncEnms.returnCode.name()));
		} catch(Exception e) {
			response.setReturnCode(LongdbDataSyncEnms.fail.name());
			response.setReturnMsg(e.getMessage());
		}
		return response;
	}

}
